/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.husong.mapper.provider;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.husong.pojo.UserWalletLogBean;
import com.husong.util.UUIDUtil;

/**
 * @author tony
 *
 * @date 2015年11月25日 下午1:44:32
 */
public class UserProvider {
	
	public String getBills(Map<String,Object> params){
		String user_id=(String)params.get("user_id");
		String lastDate=(String)params.get("lastDate");
		Long lastrowid=(Long)params.get("lastrowid");
		StringBuffer sb=new StringBuffer();
		sb.append("select e.*,e.purchase_amount+e.tip_amount as brokerAmount from hs_user_wallet_log e  where e.user_id ='"+user_id+"'");
		if(lastrowid!=null&&lastrowid!=0){
			sb.append(" AND e.row_id<"+lastrowid+"");
		}
		sb.append(" AND e.trade_time<='"+lastDate+"'");
		sb.append(" order by e.trade_time desc,e.row_id desc limit 10 ");
		return sb.toString();
		
    }
	
	public String getBillsByType(Map<String,Object> params){
		String user_id=(String)params.get("user_id");
		String lastDate=(String)params.get("lastDate");
		Long lastrowid=(Long)params.get("lastrowid");
		int type=(int)params.get("type");
		StringBuffer sb=new StringBuffer();
		sb.append("select e.*");
		if(type==1){
			sb.append(",e.purchase_amount as brokerAmount");
		}else if(type==2){
			sb.append(",e.tip_amount as brokerAmount");
			
		}else if(type==3){
			sb.append(",e.purchase_amount+e.tip_amount-e.service_amount as brokerAmount");
			
		}else if(type==4){
			sb.append(",e.purchase_amount+e.tip_amount as brokerAmount");
		}
		sb.append(" from hs_user_wallet_log e  where e.user_id ='"+user_id+"'");
		if(lastrowid!=null&&lastrowid!=0){
			sb.append(" AND e.row_id<"+lastrowid+"");
		}
		sb.append(" AND e.trade_time<='"+lastDate+"'");
		sb.append(" AND e.trade_type in (2,4,5)");
		sb.append(" order by e.trade_time desc,e.row_id desc limit 10 ");
		return sb.toString();
		
	}
	
	public String batchUpdateWithdrawOrder(Map<String, Object> param){
    	String batchOrderNo=(String)param.get("batchOrderNo");
    	List<String> ids=(List)param.get("ids");
    	StringBuffer sb = new StringBuffer();
    	sb.append(" update hs_withdraw_order set batch_order_no = ");
    	sb.append(batchOrderNo);
    	sb.append(" where withdrawOrderId in (");
    	for(String id:ids){
    		sb.append("'"+id+"'");
    		sb.append(",");
    	}
    	sb.append("'-1')");
    	return sb.toString();
    }
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String batchDeductBrokerWallet(Map<String,Object> param){
    	Map<String,BigDecimal> moneyMap=(Map<String,BigDecimal>)param.get("moneyMap");
    	List<String> ids=(List)param.get("ids");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" update hs_user_wallet b set b.frozen_amount=b.frozen_amount- case b.user_id ");
    	for(String id:ids){
    	sb.append(" when ");
    	sb.append("'"+id+"'");
    	sb.append(" then ");
    	sb.append(moneyMap.get(id));
    	}
    	
    	sb.append(" END ");
    	sb.append(" where b.user_id in (");
    	for(String id:ids){
    		sb.append("'"+id+"'");
    		sb.append(",");
    	}
    	sb.append("'-1')");
    	return sb.toString();
    }
	
	
	 @SuppressWarnings({ "unchecked", "rawtypes" })
		public String batchUpdateAliWithdrawOrder(Map<String,Object> param){
	    	Integer status =(Integer) param.get("status");
	    	Calendar finishTime =(Calendar) param.get("finishTime");
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	String batchOrderId =(String) param.get("batchOrderId");
	    	Map<String,String> reasonMap=(Map<String,String>)param.get("reasonMap");
	    	List<String> ids=(List)param.get("ids");
	    	StringBuffer sb=new StringBuffer();
	    	sb.append(" update hs_withdraw_order set finish_time = '");
	    	sb.append(sdf.format(finishTime.getTime()));
	    	sb.append("' ,batch_order_id = ");
	    	sb.append("'"+batchOrderId+"'");
	    	sb.append(" ,notify_desc = case id ");
	    	
	    	for(String id:ids){
	    		sb.append(" when ");
	        	sb.append("'"+id+"'");
	        	sb.append(" then '");
	        	sb.append("".equals(reasonMap.get(id))?"提现成功":reasonMap.get(id));
	        	sb.append("' ");
	    	}
	    	sb.append(" END ");
	    	
	    	sb.append(" ,status = ");
	    	sb.append(status);
	    	sb.append(" where id in (");
	    	for(String id:ids){
	    		sb.append("'"+id+"'");
	    		sb.append(",");
	    	}
	    	sb.append("'-1')");
	    	return sb.toString();
	    }
    
	 	@SuppressWarnings("unchecked")
		public String queryBrokerWalletLogsByOrderNo(Map<String,Object> param){
	    	List<String> orderNos=(List<String>)param.get("orderNos");
	    	StringBuffer sb=new StringBuffer();
	    	sb.append(" select * from hs_user_wallet_log l where l.order_no in (");
	    	for(String orderNo:orderNos){
	    		sb.append("'"+orderNo+"'");
	    		sb.append(",");
	    	}
	    	sb.append("'')");
	    	return sb.toString();
	    }
	 	
	 	@SuppressWarnings("unchecked")
		public String batchInsertBrokerWalletLog(Map<String,Object> param){
	    	List<UserWalletLogBean> logs=(List<UserWalletLogBean>)param.get("logs");
	    	StringBuffer sb=new StringBuffer();
	    	sb.append(" insert into hs_user_wallet_log(id,trade_type,trade_desc,order_id,trade_time,user_id,purchase_amount,tip_amount,order_no) values ");
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	for(int i=0;i<logs.size();i++){
	    		sb.append("(");
	    		sb.append("'"+UUIDUtil.getUUID()+"',");
	    		sb.append("5,");
	    		sb.append("'提现失败退款',");
	    		sb.append(logs.get(i).getOrder_id()+",");
	    		sb.append("'"+sdf.format(Calendar.getInstance().getTime())+"',");
	    		sb.append(logs.get(i).getUser_id()+",");
	    		sb.append(Math.abs(logs.get(i).getPurchase_amount().doubleValue())+",");
	    		sb.append(Math.abs(logs.get(i).getTip_amount().doubleValue())+",");
	    		sb.append("'"+logs.get(i).getOrder_no()+"'");
	    		if(i==logs.size()-1){
	    			sb.append(")");
	    		}else{
	    		sb.append("),");	
	    		}
	    	}
	    	return sb.toString();
	    }
	 	
	 	@SuppressWarnings("unchecked")
		public String batchUpdateBrokerWallet(Map<String,Object> param){
	    	Map<String,BigDecimal[]> addMap=(Map<String,BigDecimal[]>)param.get("addMap");
	    	StringBuffer sb=new StringBuffer();
	    	sb.append(" update hs_user_wallet w set w.frozen_amount = w.frozen_amount- case w.user_id ");
	    	for (Entry<String, BigDecimal[]> entry : addMap.entrySet()) {
	    		sb.append(" when ");
	        	sb.append("'"+entry.getKey()+"'");
	        	sb.append(" then ");
	        	BigDecimal[] money=entry.getValue();
	        	sb.append(Math.abs(money[0].doubleValue())+Math.abs(money[1].doubleValue()));
	    	}
	    	sb.append(" END, ");
	    	sb.append(" w.purchase_amount=w.purchase_amount+ case w.user_id");
	    	for (Entry<String, BigDecimal[]> entry : addMap.entrySet()) {
	    		sb.append(" when ");
	        	sb.append("'"+entry.getKey()+"'");
	        	sb.append(" then ");
	        	BigDecimal[] money=entry.getValue();
	        	sb.append(Math.abs(money[0].doubleValue()));
	    	}
	    	sb.append(" END, ");
	    	
	    	sb.append(" w.tip_amount=w.tip_amount+ case w.user_id");
	    	for (Entry<String, BigDecimal[]> entry : addMap.entrySet()) {
	    		sb.append(" when ");
	    		sb.append("'"+entry.getKey()+"'");
	    		sb.append(" then ");
	    		BigDecimal[] money=entry.getValue();
	    		sb.append(Math.abs(money[1].doubleValue()));
	    	}
	    	sb.append(" END ");
	    	
	    	sb.append(" where w.user_id in (");
	    	for (Entry<String, BigDecimal[]> entry : addMap.entrySet()) {
	    		sb.append("'"+entry.getKey()+"'");
	    	}
	    	sb.append(" ,'-1')");
	    	return sb.toString();
	    }
}
